package com.chinasoft.cr.controller;

import com.chinasoft.cr.mapper.VenuesMapper;
import com.chinasoft.cr.pojo.Result;
import com.chinasoft.cr.pojo.Venues;
import com.chinasoft.cr.pojo.vo.VenuesParam;
import com.chinasoft.cr.service.VenuesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * <p>
 * 场馆管理前端控制器
 * </p>
 *
 * @author pp
 * @since 2025-01-17
 */
@RestController
@CrossOrigin  // 解决跨域问题
@RequestMapping("/api/venues")
@Tag(name = "场馆管理", description = "用于管理场馆信息")
public class VenuesController {

    @Autowired
    private VenuesService venuesService;
    @Autowired
    private  VenuesMapper venuesMapper;
    /**
     * 分页查询场馆列表
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return 分页查询结果
     */
    @GetMapping(value = "/list")
    @Operation(summary = "分页获取所有场馆信息", description = "分页查询所有场馆的详细信息")
    @ApiResponse(responseCode = "200", description = "分页获取所有场馆信息成功")
    public Result list(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false)String name,
            @RequestParam(required = false)String status,
            @RequestParam(required = false, defaultValue = "0")int type,
            @RequestParam(required = false)int role
    ) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        VenuesParam p = new VenuesParam();
        p.setName(name);
        p.setStatus(status);
        p.setRole(role);
        p.setType(type);
//        Page<Venues> aPage = venuesService.page(new Page<>(pageNum, pageSize));
        Page<Venues> aPage = venuesMapper.selectPageVo(new Page<>(pageNum, pageSize), p);
        System.out.println(aPage);
        return new Result(200, "查询成功！", aPage);
    }

    /**
     * 根据ID获取场馆详情
     *
     * @param id 场馆ID
     * @return 场馆详情
     点击编辑*/
    @GetMapping(value = "/{id}")
    @Operation(summary = "根据场馆ID获取场馆信息", description = "根据场馆ID查询场馆的详细信息")
    @ApiResponse(responseCode = "200", description = "获取场馆信息成功")
    @ApiResponse(responseCode = "404", description = "获取场馆信息失败,该场馆不存在")
    public Result getById(@PathVariable("id") String id) {
        Venues venues = venuesService.getById(id);
        if (venues == null) {
            return new Result(404, "查询失败，该场馆不存在！", null);
        }
        return new Result(200, "查询成功！", venues);
    }
/*场馆名字查询*/
    @GetMapping(value = "/name/{name}")
    @Operation(summary = "根据场馆名称获取场馆信息", description = "根据场馆名称查询场馆的详细信息")
    @ApiResponse(responseCode = "200", description = "获取场馆信息成功")
    @ApiResponse(responseCode = "404", description = "获取场馆信息失败，该场馆不存在")
    public Result getByName(@PathVariable("name") String name) {
        Venues venues = venuesService.getByName(name);
        if (venues == null) {
            return new Result(404, "查询失败，该场馆不存在！", null);
        }
        return new Result(200, "查询成功！", venues);
    }
    /**
     * 根据场馆状态查询场馆信息
     * @param status 场馆状态
     * @return 查询结果
     */
    @GetMapping(value = "/status/{status}")
    @Operation(summary = "根据场馆状态获取场馆信息", description = "根据场馆状态查询场馆的详细信息")
    @ApiResponse(responseCode = "200", description = "获取场馆信息成功")
    @ApiResponse(responseCode = "404", description = "获取场馆信息失败，未找到符合条件的场馆")
    public Result getByStatus(@PathVariable("status") String status) {
        // 调用服务层方法查询场馆信息
        List<Venues> venuesList = venuesService.getByStatus(status);
        // 判断查询结果是否为空
        if (venuesList == null || venuesList.isEmpty()) {
            // 如果为空，返回404状态码和错误信息
            return new Result(404, "查询失败，未找到符合条件的场馆！", null);
        }
        // 如果不为空，返回200状态码和查询结果
        return new Result(200, "查询成功！", venuesList);
    }

    /**
     * 创建场馆
     *
     * @param params 场馆信息
     * @return 创建结果
     */
    @PostMapping(value = "/create")
    @Operation(summary = "创建场馆", description = "根据提供的场馆信息创建新场馆")
    @ApiResponse(responseCode = "200", description = "场馆创建成功")
    @ApiResponse(responseCode = "404", description = "场馆创建失败,该场馆已存在")
    public Result create(@RequestBody Venues params) {
        try {
            Boolean flag = venuesService.save(params);
            if (flag == null) {
                return new Result(500, "创建失败，未知错误！", null);
            }
            if (flag) {
                return new Result(200, "创建成功！", null);
            } else {
                return new Result(404, "创建失败，该场馆已存在！", null);
            }
        } catch (Exception e) {
            return new Result(500, "创建失败，系统错误：" + e.getMessage(), null);
        }
    }

    /**
     * 删除场馆
     *
     * @param id 场馆ID
     * @return 删除结果
     */
    @DeleteMapping(value = "/delete/{id}")
    @Operation(summary = "删除场馆", description = "根据场馆ID删除场馆")
    @ApiResponse(responseCode = "200", description = "场馆删除成功")
    @ApiResponse(responseCode = "404", description = "删除场馆失败,该场馆不存在")
    public Result delete(@PathVariable("id") String id) {
        try {
            Boolean flag = venuesService.removeById(id);
            if (flag == null) {
                return new Result(500, "删除失败，未知错误！", null);
            }
            if (flag) {
                return new Result(200, "删除成功！", null);
            } else {
                return new Result(404, "删除失败，该场馆不存在！", null);
            }
        } catch (Exception e) {
            return new Result(500, "删除失败，系统错误：" + e.getMessage(), null);
        }
    }

    /**
     * 更新场馆
     *
     * @param params 场馆信息
     * @return 更新结果
     */
    @PutMapping(value = "/update")
    @Operation(summary = "更新场馆", description = "根据提供的场馆信息更新现有场馆")
    @ApiResponse(responseCode = "200", description = "场馆更新成功")
    @ApiResponse(responseCode = "404", description = "场馆更新失败,该场馆不存在")
    public Result update(@RequestBody Venues params) {
        try {
            Boolean flag = venuesService.updateById(params);
            if (flag == null) {
                return new Result(500, "更新失败，未知错误！", null);
            }
            if (flag) {
                return new Result(200, "更新成功！", null);
            } else {
                return new Result(404, "更新失败，该场馆不存在！", null);
            }
        } catch (Exception e) {
            return new Result(500, "更新失败，系统错误：" + e.getMessage(), null);
        }
    }

}

